package com.fh.shop.api.mq.config;

import com.fh.shop.config.MQConstant;
import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class MQConfig {

    @Bean
    public DirectExchange orderEx(){
        return new DirectExchange(MQConstant.orderDirect.EX);
    }

    @Bean
    public Queue orderQueue(){
        Queue queue = new Queue(MQConstant.orderDirect.QUEUE);
        //绑定死信
        queue.addArgument("x-dead-letter-exchange",MQConstant.orderDeadDirect.EX);
        queue.addArgument("x-dead-letter-routing-key",MQConstant.orderDeadDirect.ROUTE_KEY);
        return queue;
    }

    @Bean
    public Binding orderBinding(){
        return BindingBuilder.bind(orderQueue()).to(orderEx()).with(MQConstant.orderDirect.ROUTE_KEY);
    }

    @Bean
    public DirectExchange orderDeadEx(){
        return new DirectExchange(MQConstant.orderDeadDirect.EX);
    }

    @Bean
    public Queue orderDeadQueue(){
        return new Queue(MQConstant.orderDeadDirect.QUEUE);
    }

    @Bean
    public Binding orderDeadBinding(){
        return BindingBuilder.bind(orderDeadQueue()).to(orderDeadEx()).with(MQConstant.orderDeadDirect.ROUTE_KEY);
    }


    @Bean
    public FanoutExchange payEx(){
        return new FanoutExchange(MQConstant.payFanout.EX);
    }

    @Bean
    public Queue stockQueue(){
        return new Queue(MQConstant.payFanout.STOCK_QUEUE);
    }

    @Bean
    public Queue scoreQueue(){
        return new Queue(MQConstant.payFanout.SCORE_QUEUE);
    }

    @Bean
    public Binding stockBinding(){
        return BindingBuilder.bind(stockQueue()).to(payEx());
    }

    @Bean
    public Binding scoreBinding(){
        return BindingBuilder.bind(scoreQueue()).to(payEx());
    }


    //创建交换机
    @Bean
    public DirectExchange mailEx(){
        return new DirectExchange(MQConstant.mailDirect.EX);
    }

    //创建队列
    @Bean
    public Queue mailQueue(){
        return new Queue(MQConstant.mailDirect.QUEUE);
    }

    //绑定
    @Bean
    public Binding mailBinding(){
        return BindingBuilder.bind(mailQueue()).to(mailEx()).with(MQConstant.mailDirect.ROUTE_KEY);
    }


    //新建交换机
    @Bean
    public DirectExchange directEx(){
        return new DirectExchange(MQConstant.Direct.EX);
    }

    //新建消息队列
    @Bean
    public Queue directQueue(){
        return new Queue(MQConstant.Direct.QUEUE);
    }

    //绑定交换机和消息队列
    @Bean
    public Binding directBinding(){
        return BindingBuilder.bind(directQueue()).to(directEx()).with(MQConstant.Direct.ROUTE_KEY);
    }

    @Bean
    public FanoutExchange fanoutExchange(){
        return new FanoutExchange(MQConstant.Fanout.EX);
    }

    @Bean
    public Queue fanoutQueue1(){
        return new Queue(MQConstant.Fanout.QUEUE1);
    }

    @Bean
    public Queue fanoutQueue2(){
        return new Queue(MQConstant.Fanout.QUEUE2);
    }

    @Bean
    public Binding bindingFanout1(){
        return BindingBuilder.bind(fanoutQueue1()).to(fanoutExchange());
    }

    @Bean
    public Binding bindingFanout2(){
        return BindingBuilder.bind(fanoutQueue2()).to(fanoutExchange());
    }

}
